package com.hunau.web.controller.terminal;

import java.util.ArrayList;
import java.util.List;

import com.hunau.common.core.domain.Ztree;
import com.hunau.framework.util.ShiroUtils;
import com.hunau.system.domain.SysDept;
import com.hunau.system.service.ISysDeptService;
import com.hunau.system.service.ITerminalsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.hunau.common.annotation.Log;
import com.hunau.common.enums.BusinessType;
import com.hunau.system.domain.TbEnvironment;
import com.hunau.system.service.ITbEnvironmentService;
import com.hunau.common.core.controller.BaseController;
import com.hunau.common.core.domain.AjaxResult;
import com.hunau.common.utils.poi.ExcelUtil;
import com.hunau.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 环境数据采集Controller
 * 
 * @author ruoyi
 * @date 2020-03-17
 */
@Controller
@RequestMapping("/system/environment")
public class TbEnvironmentController extends BaseController
{
    private String prefix = "terminal/environment";
    @Autowired
    private ITerminalsService terminalsService;
    @Autowired
    private ITbEnvironmentService tbEnvironmentService;
    @Autowired
    private ISysDeptService deptService;

    @RequiresPermissions("system:environment:view")
    @GetMapping()
    public String environment()
    {
        return prefix + "/environment";
    }

    @Log(title = "环境数据采集", businessType = BusinessType.IMPORT)
    @RequiresPermissions("system:environment:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<TbEnvironment> util = new ExcelUtil<TbEnvironment>(TbEnvironment.class);
        List<TbEnvironment> userList = util.importExcel(file.getInputStream());
        String operName = ShiroUtils.getSysUser().getLoginName();
        String message = tbEnvironmentService.importUser(userList, updateSupport, operName);
        return AjaxResult.success(message);
    }
    @RequiresPermissions("system:environment:view")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<TbEnvironment> util = new ExcelUtil<TbEnvironment>(TbEnvironment.class);
        return util.importTemplateExcel("环境数据");
    }
    /**
     * 查询环境数据采集列表
     */
    @RequiresPermissions("system:environment:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TbEnvironment tbEnvironment)
    {
        startPage();
        List<TbEnvironment> list = tbEnvironmentService.selectTbEnvironmentList(tbEnvironment);
        return getDataTable(list);
    }

    /**
     * 导出环境数据采集列表
     */
    @RequiresPermissions("system:environment:export")
    @Log(title = "环境数据采集", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TbEnvironment tbEnvironment, @RequestParam(value = "ids",required = false) ArrayList<Object> ids )
    {
        System.out.println(ids);
        List<TbEnvironment> list = tbEnvironmentService.selectTbEnvironmentListByIds(ids);
        ExcelUtil<TbEnvironment> util = new ExcelUtil<TbEnvironment>(TbEnvironment.class);
        return util.exportExcel(list, "environment");
    }

    /**
     * 新增环境数据采集
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存环境数据采集
     */
    @RequiresPermissions("system:environment:add")
    @Log(title = "环境数据采集", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TbEnvironment tbEnvironment)
    {
        return toAjax(tbEnvironmentService.insertTbEnvironment(tbEnvironment));
    }

    /**
     * 修改环境数据采集
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TbEnvironment tbEnvironment = tbEnvironmentService.selectTbEnvironmentById(id);
        mmap.put("tbEnvironment", tbEnvironment);
        return prefix + "/edit";
    }

    /**
     * 修改保存环境数据采集
     */
    @RequiresPermissions("system:environment:edit")
    @Log(title = "环境数据采集", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TbEnvironment tbEnvironment)
    {
        return toAjax(tbEnvironmentService.updateTbEnvironment(tbEnvironment));
    }

    /**
     * 删除环境数据采集
     */
    @RequiresPermissions("system:environment:remove")
    @Log(title = "环境数据采集", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tbEnvironmentService.deleteTbEnvironmentByIds(ids));
    }
    /**
     * 加载部门列表树
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData()
    {
        List<Ztree> ztrees = deptService.selectDeptTree(new SysDept());
        return ztrees;
    }
//    /**
//     * 加载部门列表树
//     */
//    @RequiresPermissions("system:terminals:list")
//    @GetMapping("/treeData")
//    @ResponseBody
//    public List<Ztree> treeData()
//    {
//        List<Ztree> ztrees = terminalsService.selectAddrTree(new AddrTree());
//        return ztrees;
//    }
}
